<?php
	require '../../include/library.inc.php';
	require '../../include/admin_login.inc.php';
	require '../../include/paging.inc.php';
	
	
		
		head_admin("Main Content");
	?>
			
				<?php if ($_SESSION['message']) { ?>
				<div class="message green">
					<?=$_SESSION['message']?>
				</div>
				<?php unset($_SESSION['message']); } ?>
			
			
				<div class="topbuttons">
					<a href="page_new.php">Add new</a>
					
				</div>
				<h3>Main Content</h3>
				
				<div id="loading" style="position:absolute; top:50%; left:50%; z-index:99999; display:none;"><img src="images/loading.gif"></div>
                
                <div class="cf nestable-lists" >
		
		
	
        <div class="dd" id="nestable3" >
            
            <?php 
				
				
				
		$sql = "SELECT `values`  FROM `setings` WHERE `type` = 'tabs' ";
		$setings = mysql_fetch_assoc(query($sql));
		
		 $arr =  json_c_decode($setings['values']);
		 $_SESSION['leval']  = 0;
		 
		// $sqlmax = "SELECT MAX(item_id) AS maxitem FROM menu_description WHERE menu_id = '".$menu_id."'";
		// $rowmax = mysql_fetch_object(query($sqlmax)); 
		 
		 function create_ul($ar,$first)
		 {
				$menu_id = $_GET['menu_id'];
				if($first)
				{
					echo '<ol id="cover-item" class="dd-list" >';
				}else{
				  echo '<ol class="dd-list" >';
				}
				foreach ($ar as $key => $value) {
				 
				
				$item = $value['id'];
				
				$sqlm =  "SELECT 
						p.page_id,
						p.published,
						d.title
						
					FROM 
					pages AS p,
					page_description AS d
					WHERE 
					 p.page_id = '".$item."'
					 AND p.page_id = d.page_id
					 AND d.lang_id = '1'
					 ";
			
			$rowm = mysql_fetch_object(query($sqlm)); 
				 
			$tamplate  = '<li class="dd-item dd3-item" data-id="'.$item.'" id="id-'.$item.'">
			<div class="dd-handle dd3-handle"></div><div class="dd3-content"><a  href="page_edit.php?page_id='.$item.'" >'.$rowm->title.'</a>  </div>
			<div class="act-btns">';
			
			 if ($rowm->published) { 
				$tamplate  .= '<a href="page_publish.php?page_id='.$rowm->page_id.'&publish=0" class="turn_on" name="turn_on" title="Unpublish"></a>';
			 } else { 
				$tamplate  .= '<a href="page_publish.php?page_id='.$rowm->page_id.'&publish=1" class="turn_off" name="turn_off" title="Publish"></a>';
			 } 
			
			$tamplate  .= '<a class="edit" title="Edit" href="page_edit.php?page_id='.$item.'" >Edit</a>  <a class="delete" title="Delete" href="#" onClick="deleteitem('.$item.'); return false;">Delete</a></div>';
			
	
				 
				 
				 
				 
				 echo $tamplate;
				 if($value['children']) create_ul($value['children'],false);
				 
					echo '</li>';
				}
				echo '</ol>';
				
				
		 }
		 
		 create_ul($arr,true);
					
			
			?>
                
            
        </div>

    </div>
  
  <input type="hidden" id="output" name="output"/>
  
<script type="text/javascript" src="js/jquery.nestable.js"></script>
<script type="text/javascript">

                    

$(document).ready(function()
{

    var updateOutput = function(e)
    {
        var list   = e.length ? e : $(e.target),
            output = list.data('output');
        $('#loading').show();
		$('#loading').css('top',$(document).scrollTop() + $( window).height()/2);
		$('#loading').css('left',$(document).width()/2);
		if (window.JSON) {
            //output.val(window.JSON.stringify(list.nestable('serialize')));//, null, 2));
			
			 $.ajax({
					type: 'POST',
					url: 'ajax_setings.php',
					data: {data : window.JSON.stringify(list.nestable('serialize')) , type : 'tabs'} ,
					success: function(msg){
						
						$('#loading').delay(1000).hide(100);
					},
					error: function(msg){
						alert('Err:'+ msg)
					}
				});
			
			
        } else {
            alert('JSON browser support required for this.');
        }
    };

   

    $('#nestable-menu').on('click', function(e)
    {
        var target = $(e.target),
            action = target.data('action');
        if (action === 'expand-all') {
            $('.dd').nestable('expandAll');
        }
        if (action === 'collapse-all') {
            $('.dd').nestable('collapseAll');
        }
    });

    $('#nestable3').nestable();
	
	
	// activate Nestable for list 2
    $('#nestable3').nestable({
        group: 1
    })
    .on('change', updateOutput);

    // output initial serialised data
    updateOutput($('#nestable3').data('output', $('#output')));
	
	


});


var menuitems = (<?=$rowmax->maxitem?>+1);


function deleteitem(id)
{
	var answer = confirm("Warning! All related elements will also be deleted. Do you want to continue?")
		if (answer){
			  $('#id-'+id).remove();
			  $('#nestable3').change();
		}
}

function edititem(id)
{
	$('#input'+id).toggle(200);
}

function senditem()
{
	var str = '';
	$( ".dd-item" ).each(function() {
		
		str += '["'+$( this ).attr('data-id');
		var ie = $( this ).attr('data-id');
  		
		$( '#input'+ie+' input').each(function() {
			str += '","'+ $( this ).val();
		});
		str += '"],';
	});
	
	
	$('#titledata').val(str);
	$('#nestable3').change();
	 
	$('#new_menu').submit();
}
</script>
                
                
            
<?php
	foot_admin(); 
?>